import { isIdCard, isMob, isPhone, isUnifyTheSCC, validateIsemail } from '@fl/utils/validate'
import { reactive } from 'vue'

export function useRule(vm?: any) {
    const validatorShenfzh = (rule, value, callback) => {
        if (!isIdCard(value))
            callback(new Error('请输入正确的身份证号'))
        else callback()
    }

    const validatorMobile = (rule, value, callback) => {
        if (isMob(value) || isPhone(value)) {
            callback()
        }
        else {
            callback(new Error('请输入正确的注册号码'))
        }
    }

    const validatorShoujhm = (rule, value, callback) => {
        if (!isPhone(value))
            callback(new Error('请输入正确的手机号码'))
        else callback()
    }

    const validatorTongyxydm = (rule, value, callback) => {
        if (!isUnifyTheSCC(value))
            callback(new Error('请输入正确的统一社会信用代码'))
        else callback()
    }

    const validatorSfz = (rule, value, callback) => {
        const { idCardBack, idCardFront } = vm.exposed.form.value

        if (!idCardFront) {
            callback(new Error('请上传身份证正面'))
        }
        if (!idCardBack) {
            callback(new Error('请上传身份证反面'))
        }
        if (idCardBack && idCardFront)
            callback()
    }

    const validatorBus = (rule, value, callback) => {
        const { businessLicenseAttachmentsList } = vm.exposed.form.value
        if (businessLicenseAttachmentsList && businessLicenseAttachmentsList.length)
            callback()
        else
            callback(new Error('请上传营业执照'))
    }

    const validatorAtt = (rule, value, callback) => {
        const { attachmentsAuthorizationLetterList } = vm.exposed.form.value
        if (attachmentsAuthorizationLetterList && attachmentsAuthorizationLetterList.length)
            callback()
        else
            callback(new Error('请上传授权函'))
    }

    const validatorType = (rule, value, callback) => {
        const { enterpriseSecondType, enterpriseType } = vm.exposed.form.value

        if (enterpriseSecondType)
            callback()
        else
            callback(new Error(`请选择${enterpriseType === '0' ? `采购商类型` : `供应商类型`}`))
    }

    const validatorCpt = (rule, value, callback) => {
        const reg = /^\d{1,8}$/
        if (!reg.test(value))
            callback(new Error('请输入验证码'))
        else callback()
    }

    const rules = reactive<any>({
        accountName: [
            { message: '请输入开户名称', required: true, trigger: 'blur' },
        ],
        accountNumber: [
            { message: '请输入银行账户', required: true, trigger: 'blur' },
        ],
        attachmentsAuthorizationLetterList: [
            { required: true, trigger: 'blur', validator: validatorAtt },
        ],
        bankName: [
            { message: '请输入开户银行', required: true, trigger: 'blur' },
        ],
        businessLicenseAttachmentsList: [
            { required: true, trigger: 'blur', validator: validatorBus },
        ],
        captcha: [
            { required: true, trigger: 'blur', validator: validatorCpt },
        ],
        companyName: [
            { message: '请输入公司名称', required: true, trigger: 'blur' },
        ],
        contactCardNo: [
            { required: true, trigger: 'blur', validator: validatorShenfzh },
        ],
        contactEmail: [
            { required: true, trigger: 'blur', validator: validateIsemail },
        ],
        contactMobile: [
            { required: true, trigger: 'blur', validator: validatorShoujhm },
        ],
        contactName: [
            { message: '请输入姓名', required: true, trigger: 'change' },
        ],
        enterpriseSecondType: [
            { required: true, trigger: 'change', validator: validatorType },
        ],
        enterpriseType: [
            { message: '请选择企业类型', required: true, trigger: 'change' },
        ],
        fdsfz: [
            { required: true, trigger: 'change', validator: validatorSfz },
        ],
        legalPersonCardNo: [
            { required: true, trigger: 'blur', validator: validatorShenfzh },
        ],
        legalPersonName: [
            { message: '请输入法人姓名', required: true, trigger: 'blur' },
        ],
        legalPersonPhone: [
            { required: true, trigger: 'blur', validator: validatorShoujhm },
        ],
        legalRepresentative: [
            { message: '请输入法定代表人', required: true, trigger: 'change' },
        ],
        registeredAddress: [
            { message: '请输入注册地址', required: true, trigger: 'change' },
        ],
        registeredMobile: [
            { required: true, trigger: 'change', validator: validatorMobile },
        ],
        supplyCat: [
            { message: '请选择供应商类别', required: true, trigger: 'change' },
        ],
        unifiedSocialCreditCode: [
            { required: true, trigger: 'change', validator: validatorTongyxydm },
        ],
    })

    return { rules }
}
